package com.itheima.reggie.controller.front;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.itheima.reggie.annotations.ItheimaCacheable;
import com.itheima.reggie.common.Result;
import com.itheima.reggie.entity.DishDto;
import com.itheima.reggie.entity.Setmeal;
import com.itheima.reggie.entity.SetmealDto;
import com.itheima.reggie.service.SetmealDishService;
import com.itheima.reggie.service.SetmealService;
import com.itheima.reggie.util.LoginUserUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 套餐管理
 */
@RestController
@RequestMapping("/fronts/setmeal")
@Slf4j
public class FSetmealController {
    @Autowired
    private SetmealService setmealService;
    @Autowired
    private SetmealDishService setmealDishService;

    @Autowired
    private LoginUserUtil loginUserUtil;

    /**
     * 分页查询
     * @param page
     * @param pageSize
     * @return
     */
    @GetMapping("/page")
    public Result<JSONObject> page(Integer page, Integer pageSize,String name){
        return setmealService.findPage(page,pageSize,name);
    }

    @PostMapping
    public Result<String> save(@RequestBody SetmealDto setmealDto){
        log.info("套餐信息：{}",setmealDto);

        setmealService.saveWithDish(setmealDto);
        return Result.success("新增套餐成功");
    }

    /**
     * 删除套餐
     * @param ids
     * @return
     */
    @DeleteMapping
    public Result<String> delete(@RequestParam List<Long> ids){
        Object user = loginUserUtil.getFrontUser();

        log.info("ids:{}",ids);
        return  setmealService.delete(ids,Long.parseLong(user.toString()));
    }

    /**
     * 启售或停售套餐
     * @param ids
     * @return
     */
    @PostMapping("/status/{status}")
    public Result<String> status(@RequestParam List<Long> ids,@PathVariable Integer status){
        Object user = loginUserUtil.getFrontUser();
        //dishService.editStatus(status,ids);
        setmealService.editStatus(status,ids,Long.parseLong(user.toString()));
        return Result.success("修改菜品成功");
    }

    /**
     * 修改套餐
     * @param setmealDto
     * @return
     */
    @PutMapping
    public Result<String> update(@RequestBody SetmealDto setmealDto){
        log.info(setmealDto.toString());
        //dishService.updateWithFlavor(dishDto);
        setmealService.updateWithDish(setmealDto);
        return Result.success("修改菜品成功");
    }

    /**
     * 根据id查询菜品信息和对应的口味信息
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    @ItheimaCacheable(value = "setmeal",key = "#id")
    public Result<SetmealDto> get(@PathVariable String id){
        SetmealDto setmealDto = setmealService.getByIdWithDish(Long.parseLong(id));
        return Result.success(setmealDto);
    }

    /**
     * 根据条件查询套餐数据
     * @param setmeal
     * @return
     */
    @GetMapping("/list")
    public Result<List<Setmeal>> list(Setmeal setmeal){
        LambdaQueryWrapper<Setmeal> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(setmeal.getCategoryId() != null,Setmeal::getCategoryId,setmeal.getCategoryId());
        queryWrapper.eq(setmeal.getStatus() != null,Setmeal::getStatus,setmeal.getStatus());
        queryWrapper.orderByDesc(Setmeal::getUpdateTime);

        List<Setmeal> list = setmealService.list(queryWrapper);
        return Result.success(list);
    }

}
